#include "cgeSocketUtils.h"

namespace basterd
{
	namespace net
	{
//		// ----------------------------------------------------------------------------
//		// SocketUtils
//		// ----------------------------------------------------------------------------
//		bool SocketUtils::setKeepAlive( __socket sid, bool enabled )
//		{
//			int value = enabled;
//			int size = sizeof(value);
///*
//			if( setsockopt(sid, SOL_SOCKET, SO_KEEPALIVE, (char*)&value, size) == SOCKET_ERROR)
//			{
//				CGE_WARNF("Cannot socket's keepalive as "
//							<< ( enabled ? "ON" : "OFF" )
//							<<  " (is this really a TCP Server Socket??)" );
//
//				return false;
//			}
//*/
//			return true;
//		}
//		// ----------------------------------------------------------------------------
//		bool SocketUtils::getKeepAlive( __socket sid )
//		{
//			int iOptVal;
//			int iOptLen = sizeof(int);
///*
//			if (getsockopt(sid, SOL_SOCKET, SO_KEEPALIVE, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR)
//			{
//				return iOptVal != 0;
//			}*/
//
//			return false;
//		}
//		// ----------------------------------------------------------------------------
//		bool SocketUtils::setBroadcast( __socket sid, bool enabled )
//		{
//			int optval = enabled;
//			unsigned int optlen = sizeof(optval);
///*
//			if( setsockopt(sid, SOL_SOCKET, SO_BROADCAST, (char*)&optval,optlen) == SOCKET_ERROR )
//			{
//				CGE_WARNF("Cannot socket's broadcasting as "
//						  << ( enabled ? "ON" : "OFF" )
//						  <<  " (is this really an UDP socket??)" );
//
//				return false;
//			}*/
//
//			return true;
//		}
//		// ----------------------------------------------------------------------------
//		bool SocketUtils::getBroadcast( __socket sid )
//		{
//			int iOptVal;
//			int iOptLen = sizeof(int);
///*
//			if (getsockopt(sid, SOL_SOCKET, SO_BROADCAST, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR)
//			{
//				return iOptVal != 0;
//			}*/
//
//			return false;
//		}
//		// ----------------------------------------------------------------------------
//		bool SocketUtils::setReuseAddress( __socket sid, bool enabled )
//		{
//#if CGE_PLATFORM == CGE_LINUX || CGE_PLATFORM == CGE_LIN64 || CGE_PLATFORM == CGE_APPLE
//			int on =  enabled? 1 : 0;
//			socklen_t optlen = sizeof(int);
//
//			return ( !( setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, optlen ) == -1) );
//#elif CGE_PLATFORM == CGE_WIN32 || CGE_PLATFORM == CGE_WIN64
//			int on =  enabled? 1 : 0;
//			socklen_t optlen = sizeof(int);
//
//			return ( !( setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, (const char*) &on, optlen ) == -1) );
//#endif
//		}
//		// ----------------------------------------------------------------------------
//		bool SocketUtils::getReuseAddress( __socket sid )
//		{
//			int iOptVal;
//			int iOptLen = sizeof(int);
///*
//			if ( ::getsockopt(sid, SOL_SOCKET, SO_REUSEADDR, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR )
//			{
//				return iOptVal != 0;
//			}*/
//
//			return false;
//		}
//		// ----------------------------------------------------------------------------
//		bool SocketUtils::setNonBlocking( __socket socketId, bool noblock )
//		{
//		#if (CGE_PLATFORM == CGE_APPLE) || (CGE_PLATFORM == CGE_LINUX) || (CGE_PLATFORM == CGE_LIN64)
//
//			int nonBlocking = noblock ? 1 : 0;
//			if ( fcntl( socketId, F_SETFL, O_NONBLOCK, nonBlocking ) == -1 )
//			{
//				if( noblock )
//				{
//					CGE_ERR( "Failed to set non-blocking socket" );
//				}else
//				{
//					CGE_ERR( "Failed to set blocking socket" );
//				}
//
//				return false;
//			}
//		#elif CGE_PLATFORM == CGE_WIN32
//
//			DWORD nonBlocking = noblock ? 1 : 0;
//			if ( ioctlsocket( socketId, FIONBIO, &nonBlocking ) != 0 )
//			{
//				if( noblock )
//				{
//					CGE_ERR( "Failed to set non-blocking socket" );
//				}else
//				{
//					CGE_ERR( "Failed to set blocking socket" );
//				}
//
//				return false;
//			}
//		#endif
//
//			return true;
//		}
//		// ----------------------------------------------------------------------------
//		bool SocketUtils::getNonBlocking( __socket sid )
//		{
//			int iOptVal;
//			int iOptLen = sizeof(int);
//
//			/*
//			if ( ::getsockopt(sid, SOL_SOCKET, SO_BROADCAST, (char*)&iOptVal, &iOptLen) != SOCKET_ERROR)
//			{
//				return iOptVal != 0;
//			}*/
//
//			return false;
//		}
//		// ----------------------------------------------------------------------------
	}
}
